#include "JsonAnalysis.h"
#include "json/json.h"
#include "MyManager.h"
#include <android/log.h>

#define  LOG_TAG    "ClientSdk"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)

bool CJsonAnalysis::Heartbet(string &strJson)
{
	strJson = "";
	Json::Value item;
	Json::FastWriter writer;
	item["iCmdEnum"] = CMD_HEARTBEAT_REQ;

	strJson = writer.write(item);

	//LOGI("CJsonAnalysis::Heartbet strJson:%s (%i)", strJson.c_str(), __LINE__);

	return true;
}

bool CJsonAnalysis::ChannelDeclare(string &strJson, int nChannelType, int nClientType, int nClientId)
{
	strJson = "";
	Json::Value root;

	root["iCmdEnum"] = CMD_T2S_CHANNELDECLAR_REQ;
	root["iTerminalType"] = nClientType;
	root["iTerminalID"] = nClientId;
	root["iChannelType"] = nChannelType;

	strJson = root.toStyledString();

	return true;
}

bool CJsonAnalysis::AnalysChannelDeclareRsp(const string &strJson, int &nResult, string &strError)
{
	Json::Reader reader;
	Json::Value value;

	if (reader.parse(strJson, value))
	{
		nResult = value["iResult"].asInt();
		strError = value["strErrorMsg"].asString();

		return true;
	}
	else
		return false;	
}

bool CJsonAnalysis::UserLogin(string &strJson, const char *pUsrName, const char *pPassWord)
{
	strJson = "";
	Json::Value root;

	root["iCmdEnum"] = CMD_T2S_LOGIN_REQ;
	root["strUserName"] = pUsrName;
	root["strPassWord"] = pPassWord;

	strJson = root.toStyledString();

	return true;
}

bool CJsonAnalysis::AnalysUserLoginRsp(const string &strJson, ReqRspInfo *pRsp)
{
	Json::Reader reader;
	Json::Value value;

	if (reader.parse(strJson, value))
	{
		pRsp->nResult = value["iResult"].asInt();
		pRsp->strError = value["strErrorMsg"].asString();

		return true;
	}
	else
		return false;
}

bool CJsonAnalysis::AnalysIssuedMeeting(const string &strJson, MeetingInfo *pMeetingInfo)
{
	Json::Reader reader;
	Json::Value value;

	if (reader.parse(strJson, value))
	{
		pMeetingInfo->nMeetingId = value["iMeetingID"].asInt();
		pMeetingInfo->nChairManId = value["iChairmanID"].asInt();
		pMeetingInfo->strName = value["strName"].asString();

		pMeetingInfo->nType = value["iType"].asInt();
		pMeetingInfo->strSlogan = value["strSlogan"].asString();
		pMeetingInfo->strStartTime = value["strStartTime"].asString();

		pMeetingInfo->strEndTime = value["strEndTime"].asString();
		pMeetingInfo->strFileServerIP = value["strFileServerIP"].asString();
		pMeetingInfo->nFileServerPort = value["iFileServerPort"].asInt();

		return true;
	}
	else
		return false;
}

bool CJsonAnalysis::TerminalGetUserInfoReq(string &strJson, int nTerminalId)
{
	strJson = "";
	Json::Value root;

	root["iCmdEnum"] = CMD_T2S_USERINFO_REQ;
	root["iTerminalID"] = nTerminalId;

	strJson = root.toStyledString();

	return true;
}

bool CJsonAnalysis::TerminalGetUserListReq(string &strJson)
{
	strJson = "";
	Json::Value root;

	root["iCmdEnum"] = CMD_T2S_USERLIST_REQ;

	strJson = root.toStyledString();

	return true;
}

bool CJsonAnalysis::TerminalGetMeetingFileListReq(string &strJson)
{
	strJson = "";
	Json::Value root;

	root["iCmdEnum"] = CMD_T2S_FILELIST_REQ;

	strJson = root.toStyledString();

	return true;
}

bool CJsonAnalysis::TerminalGetMeetingIssueListReq(string &strJson)
{
	strJson = "";
	Json::Value root;

	root["iCmdEnum"] = CMD_T2S_ISSUELIST_REQ;

	strJson = root.toStyledString();

	return true;
}

bool CJsonAnalysis::TerminalGetMeetingVoteListReq(string &strJson)
{
	strJson = "";
	Json::Value root;

	root["iCmdEnum"] = CMD_T2S_GETVOTE_REQ;

	strJson = root.toStyledString();

	return true;
}

bool CJsonAnalysis::UserSignControl(string &strJson, int nControlType)
{
	strJson = "";
	Json::Value root;

	root["iCmdEnum"] = CMD_T2S_SIGNCONTROL;
	root["iControlType"] = nControlType;

	strJson = root.toStyledString();

	return true;
}

bool CJsonAnalysis::IssueControl(string &strJson, int nControlType, int nIssueId)
{
	strJson = "";
	Json::Value root;

	root["iCmdEnum"] = CMD_T2S_ISSUECONTROL;
	root["iControlType"] = nControlType;
	root["iIssueID"] = nIssueId;

	strJson = root.toStyledString();

	return true;
}

bool CJsonAnalysis::VoteControl(string &strJson, int nControlType, int nVoteId)
{
	strJson = "";
	Json::Value root;

	root["iCmdEnum"] = CMD_T2S_VOTECONTROL;
	root["iControlType"] = nControlType;
	root["iVoteID"] = nVoteId;

	strJson = root.toStyledString();

	return true;
}

bool CJsonAnalysis::CenterControlMsg(string &strJson, int nControlType)
{
	strJson = "";
	Json::Value root;

	root["iCmdEnum"] = CMD_T2S_CENTERCONTROL;
	root["iControlType"] = nControlType;

	strJson = root.toStyledString();

	return true;
}

bool CJsonAnalysis::FileStatus(string &strJson, const char *pFileName, int nTotalSize, int nCurrentSize)
{
	strJson = "";
	Json::Value root;

	root["FileName"] = pFileName;
	root["TotalFileSize"] = nTotalSize;
	root["CurrentFileSize"] = nCurrentSize;

	strJson = root.toStyledString();

	return true;
}

bool CJsonAnalysis::TerminalUploadFile(string &strJson, TerminalFileInfo* pTerminalFileInfo)
{
	strJson = "";
	Json::Value root;

	root["iCmdEnum"] = CMD_T2S_UPLOADFILE;
	root["iMeetRoomID"] = pTerminalFileInfo->nMeetingRoomId;
	root["strFileName"] = pTerminalFileInfo->strFileName.c_str();

	root["iFileSize"] = pTerminalFileInfo->nFileSize;
	root["strFilePath"] = pTerminalFileInfo->strFilePath.c_str();
	root["iFileType"] = pTerminalFileInfo->nFileType;

	root["iTerminalID"] = pTerminalFileInfo->nTerminalId;
	root["iUserID"] = pTerminalFileInfo->nUserId;

	strJson = root.toStyledString();

	return true;
}

bool CJsonAnalysis::TerminalUserSign(string &strJson, int nTerminalId, int nUserId, const string &strUserName, const string &strRemotePath)
{
	strJson = "";
	Json::Value root;

	root["iCmdEnum"] = CMD_T2S_USERSIGN;
	root["iTerminalID"] = nTerminalId;
	root["iUserID"] = nUserId;

	root["strUserName"] = strUserName.c_str();
	root["strFilePath"] = strRemotePath.c_str();

	strJson = root.toStyledString();

	return true;
}
